package com.youbo.cloudmall.search.controller;

import com.youbo.cloudmall.search.service.ProductSaveService;
import com.youbo.common.exception.BizCodeEnum;
import com.youbo.common.to.es.SkuEsModel;
import com.youbo.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author youbo
 * @version 1.0
 * @date 2021/10/11 19:58
 */
@RequestMapping("/search")
@RestController
@Slf4j
public class ElasticSearchController {

    @Autowired
    private ProductSaveService productSaveService;

    //上架商品
    @PostMapping("/product/save")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean flag = false;
        try {
            flag = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSearchController商品上架错误{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if (flag) return R.ok();
        return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
    }
}
